//
//  RGMessageViewController.m
//  RedGrass
//
//  Created by wukong on 2018/11/24.
//  Copyright © 2018年 hongcaosp. All rights reserved.
//

#import "RGMessageViewController.h"
#import "RGMessageView.h"
#import "RGMessageContactVC.h"
#import "RGMessageBaseTableVC.h"
#import "RGMessageFunsVC.h"
#import "RGMessagePraiseVC.h"
#import "RGMessageCommentVC.h"
#import "RGMessageInterfaceBulider.h"
#import "RGMessageContactModel.h"
#import "RGChatViewController.h"


@interface RGMessageViewController ()<RGMessageViewViewDelegate>

@property (strong, nonatomic) RGMessageView *messageView;
@property (nonatomic,strong) NSMutableArray *dataList;
@property (nonatomic,assign) NSInteger pageNum;

@end

@implementation RGMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isHiddenNavBar = YES;
    [self.customContentView addSubview:self.messageView];
    [_messageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    
    [_messageView beginRefreshing];
}

- (void)loadFirstPageWithView:(RGMessageView *)view pageNum:(NSInteger)pageNum {
    HttpRequest *quest = [HttpRequest buildRequest:url_messageList];
    NSDictionary *params = [RGMessageInterfaceBulider bulidMessageListWithType:List pageNumber:self.pageNum pageSize:PageSize];
    AA_WEAK_MAKE(self) weakSelf = self;
    [quest post:params success:^(id data) {
        [weakSelf.messageView headerEndRefreshing];
        
        weakSelf.dataList = [RGMessageContactModel arrayOfModelsFromDictionaries:[data objectForKey:@"list"] error:nil];
        [weakSelf.messageView reloadTableView:weakSelf.dataList];
        
        if (weakSelf.dataList.count < PageSize) {
            [weakSelf.messageView footerEndRefreshingWithNoData];
        }
    } fail:^(NSInteger code, NSString *message) {
        [weakSelf.messageView headerEndRefreshing];
        [weakSelf showTextMessageWithMessage:message];
    }];
}

- (void)loadNextPageWithView:(RGMessageView *)view pageNum:(NSInteger)pageNum {
    HttpRequest *quest = [HttpRequest buildRequest:url_messageList];
    NSDictionary *params = [RGMessageInterfaceBulider bulidMessageListWithType:List pageNumber:self.pageNum pageSize:PageSize];
    AA_WEAK_MAKE(self) weakSelf = self;
    [quest post:params success:^(id data) {
        [weakSelf.messageView footerEndRefreshing];
        NSArray *list = [RGMessageContactModel arrayOfModelsFromDictionaries:[data objectForKey:@"list"] error:nil];
        if (list) {
            [weakSelf.dataList addObjectsFromArray:list];
            [weakSelf.messageView reloadTableView:weakSelf.dataList];
        }
        if (list.count < PageSize) {
            [weakSelf.messageView footerEndRefreshingWithNoData];
        }
    } fail:^(NSInteger code, NSString *message) {
        [weakSelf.messageView footerEndRefreshing];
        [weakSelf showTextMessageWithMessage:message];
    }];
}

- (void)contactBtnChosedWithView:(RGMessageView *)view {
    RGMessageContactVC *vc = [[RGMessageContactVC alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)cellChosedWithView:(RGMessageView *)view chosedIndexPath:(NSIndexPath *)indexPath {
    if([_dataList count]){
        RGMessageContactModel *model = _dataList[indexPath.row];
        NSInteger userId = [RGUserInfoModel getUserInfo].ID;
        if(userId == model.ID){
            [self showTextMessageWithMessage:@"不能和自己发私信"];
        }else{
            RGChatViewController *vc = [[RGChatViewController alloc]init];
            vc.userId = [RGUserInfoModel getUserInfo].ID;
            vc.toUserId = model.ID;
            vc.nickName = model.nickname;
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
      
    }
}

- (void)funcItemChosedWithView:(RGMessageView *)view chosedTag:(NSInteger)tag {
    RGMessageBaseTableVC *vc;
    switch (tag) {
        case 10000: {
            vc = [[RGMessageFunsVC alloc] init];
            vc.type = Funs;
        }
            break;
        case 10001: {
            vc = [[RGMessagePraiseVC alloc] init];
            vc.type = Praise;
        }
            break;
        case 10002: {
            vc = [[RGMessageAtMeVC alloc] init];
            vc.type = AtMe;
            
        }
            
            break;
        case 10003: {
            vc = [[RGMessageCommentVC alloc] init];
            vc.type = Comment;
            
        }
            break;
        default:
            break;
    }
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - getter
-(RGMessageView *)messageView{
    if(!_messageView){
        _messageView = [[RGMessageView alloc]init];
        _messageView.delegate = self;
    }
    return _messageView;
}

- (NSMutableArray *)dataList {
    if (!_dataList) {
        _dataList = [NSMutableArray new];
    }
    return _dataList;
}

@end
